
import { Component, OnInit, ViewChild } from '@angular/core';
import 'rxjs/add/operator/map';
import { CommService } from '../../comm/comm.service';
import { SubArea } from '../../model/report/sub-area';
import { QueryModel } from '../../model/report/query-model';
import 'rxjs/add/operator/toPromise';
declare const $: any;
import { TVResource } from 'app/ptpmc/model/comm/tv-resource';
import { DxDropDownBoxComponent } from 'devextreme-angular';
import { ActivatedRoute } from '@angular/router';
import { DatePipe } from '@angular/common';

@Component({
    selector: 'app-config-wp',
    templateUrl: './ct-desc.component.html',
    styleUrls: ['./ct-desc.component.scss'],
    providers: [DatePipe],
})

export class CtDescComponent implements OnInit {
    title = '额定CT及型号描述配置';

    /*生产线 - 班次*/
    public subAreaItems: SubArea[];
    public shiftItems: any[];
    public equipemntItems: TVResource[] = [];
    public subAareSelected: any;
    public shiftSelected: any;
    public equipmentSelected: TVResource[];

    public items: any[];
    public timeItems: any[];

    public subAreaItems1: SubArea[];
    public subAareSelected1: any;
    public timeRange = '';
    sxTarget = 0;
    xxTarget = 0;

    @ViewChild('dddb') dddb: DxDropDownBoxComponent;

    // constructor(private commServices: CommService) {
    // }

    constructor(private commServices: CommService,
        public route: ActivatedRoute,private datePipe:DatePipe) {
          
    }
    ngOnInit() {
        this.shiftItems = this.commServices.shifts;
        this.shiftSelected = this.shiftItems[0];
        this.subAreaItems = this.commServices.getSubAreas();
        this.subAareSelected = this.subAreaItems[0];
        this.equipemntItems = this.commServices.getTVResourceBySubArea(this.subAreaItems[0].subArea);
        this.equipmentSelected = this.equipemntItems;

        this.subAreaItems1 = this.commServices.getSubAreas();
        this.subAareSelected1 = this.subAreaItems1[0];

        this.commServices.initDatePicker($('#dateTimeRange'), 'YYYY-MM-DD HH:mm:ss');
    }

    clearEquipmentSelected() {
        this.equipmentSelected = [];
    }
    equipmentSelectedOk() {
        this.dddb.instance.close();
    }

    onSubAreaSelectedChange(subArea: any): void {
        this.equipemntItems = this.commServices.getTVResourceBySubArea(subArea.value.subArea);
        this.equipmentSelected = this.equipemntItems;
    }
    /**
     * 获取表格数据
     */
    getReportData(): void {
        const queryModel: QueryModel = new QueryModel();
        queryModel.querySubAreaIds = [this.subAareSelected.subArea];
        queryModel.queryResourceIds = this.equipmentSelected.map(e => e.equipment);
        this.commServices
            .getList(queryModel, '/partct/list')
            .subscribe(model => {
                if (model.resultCode === 200) {
                    this.items = model.data;
                    this.items.forEach(e => {
                        e.generatedTime = this.datePipe.transform(e.generatedTime, 'yyyy-MM-dd HH:mm:ss');
                    });
                } else { 
                    alert(model.resultCode + ',' + model.resultMessage);
                }
            });
    }

    updateDetail() {
        this.commServices
            .getList(this.items, '/partct/updatep')
            .subscribe(model => {
                if (model.resultCode === 200) {
                    alert(model.resultMessage);
                } else {
                    alert(model.resultCode + ',' + model.resultMessage);
                }
            });
    }

    getTime(id: any): String {
        return $('#' + id).val();
    }
}
